package com.cskaoyan.service;

import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.CouponMapper;
import com.cskaoyan.mapper.CouponUserMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wuyuchen
 * Date: 2020/6/30 10:56
 */
@Service
public class CouponServiceImpl implements CouponService {
    @Autowired
    CouponMapper couponMapper;
    @Autowired
    CouponUserMapper couponUserMapper;

    @Override
    public Map<String, Object> CouponList(CouponResponseVo couponResponseVo) {
        // 开启分页
        PageHelper.startPage(couponResponseVo.getPage(), couponResponseVo.getLimit());
        CouponExample couponExample = new CouponExample();
        CouponExample.Criteria criteria = couponExample.createCriteria();
        // 排序
        couponExample.setOrderByClause(couponResponseVo.getSort() + " " + couponResponseVo.getOrder());
        // 若查询参数里面的名字非空，则添加查询名字的条件
        if(couponResponseVo.getName() != null && !"".equals(couponResponseVo.getName())) {
            criteria.andNameLike("%" + couponResponseVo.getName() + "%");
        }
        // 若查询参数里面的优惠券类型非空，则添加查询类型的条件
        if(couponResponseVo.getType() != null) {
            criteria.andTypeEqualTo(couponResponseVo.getType());
        }
        // 若查询参数里面的优惠券状态非空，则添加查询状态的条件
        if(couponResponseVo.getStatus() != null) {
            criteria.andStatusEqualTo(couponResponseVo.getStatus());
        }

        // 最后记得加上 delete == false 才能显示给用户
        criteria.andDeletedEqualTo(false);

        List<Coupon> couponList = couponMapper.selectByExample(couponExample);
        // 获取查询的总条数
        long total = couponList.size();

        HashMap<String, Object> map = new HashMap<>();
        map.put("total", total);
        map.put("items", couponList);

        return map;
    }

    @Override
    public Coupon addCoupon(Coupon coupon) {
        Date date = new Date();
        coupon.setAddTime(date);
        coupon.setUpdateTime(date);
        coupon.setDeleted(false);

        couponMapper.insert(coupon);
        Integer id = couponMapper.queryLastInsertId();
        Coupon couponNew = couponMapper.selectByPrimaryKey(id);
        return couponNew;
}

    @Override
    public Coupon readCouponById(Integer id) {
        Coupon coupon = couponMapper.selectByPrimaryKey(id);
        return coupon;
    }

    @Override
    public Map<String, Object> getCouponUsers(CouponUserListRequestVo requestVo) {
        PageHelper.startPage(requestVo.getPage(), requestVo.getLimit());
        CouponUserExample couponUserExample = new CouponUserExample();
        CouponUserExample.Criteria criteria = couponUserExample.createCriteria();

        // 设置排序
        couponUserExample.setOrderByClause(requestVo.getSort() + " " + requestVo.getOrder());
        // 设置搜索条件
        if (requestVo.getUserId() != null) {
            criteria.andUserIdEqualTo(requestVo.getUserId());
        }
        if (requestVo.getStatus() != null) {
            criteria.andStatusEqualTo(requestVo.getStatus());
        }
        // 设置在该优惠券的id下进行查询
        criteria.andCouponIdEqualTo(requestVo.getCouponId());
        // 设置只返回delete = false的记录
        criteria.andDeletedEqualTo(false);

        List<CouponUser> couponUserList = couponUserMapper.selectByExample(couponUserExample);
        int size = couponUserList.size();
        // 封装结果
        HashMap<String, Object> map = new HashMap<>();
        map.put("total",size);
        map.put("items",couponUserList);
        return map;
    }

    @Override
    public Coupon updateCoupon(Coupon coupon) {
        couponMapper.updateByPrimaryKey(coupon);
        Coupon couponNew = couponMapper.selectByPrimaryKey(coupon.getId());
        return couponNew;
    }

    @Override
    public int deleteCoupon(Coupon coupon) {
        // 更新删除的时间
        Date date = new Date();
        coupon.setUpdateTime(date);

        couponMapper.delete(coupon);
        return 0;
    }
}
